from 查找没用的图片 import listDir
from functools import reduce
def searchText(file,text1,text2="zhengwutong.request"):
    with open(file, "r") as f:
        return [x for x in enumerate(f.readlines()) if x[1].find(text1) != -1 and x[1].find(text2) != -1 and x[1].find(",placeHolder11:1") == -1]
def searchText2(file,text):
    with open(file, "r") as f:
        return [x for x in enumerate(f.readlines()) if x[1].find(text) != -1]
def catDirs(dirs):
    return reduce(lambda x, y: x + y, dirs)
def listDirs(paths,extensions):
    return reduce(lambda x, y: x + y, [listDir(p,extensions=extensions) for p in paths])
fitFiles = listDirs(paths=["/Users/joker/Workplace/zhengwutong3/zhengwutong/Modules"],extensions=["swift"])
# print(len([print(x) for x in fitFiles]))

# [print(f'共{len([print("{0} {1}".format(x[0],x[1])) for x in searchText(p,".request(")])}条，文件：{p}\n\n\n\n') for p in [] + ["/Users/joker/Workplace/zhengwutong3/zhengwutong/appDelegate.swift"] if len(searchText(p,".request("))>0]
# [print(f'共{len([print("{0} {1}".format(x[0],x[1])) for x in searchText(p,".request(")])}条，文件：{p}\n\n\n\n') for p in [] + ["/Users/joker/Workplace/zhengwutong3/zhengwutong/appDelegate.swift"] if len(searchText(p,".request("))>0]

# text2 = "self.goverId ?? "+'""'
# text2 = "SSelf.goverId ?? "+'""'
# text2 = "goverId"

# text2 = "self.bookId ?? "+'""'
# text2 = "self.bookId"
# text2 = "SSelf.bookId ?? "+'""'
# text2 = "SSelf.bookId"
text2 = "bookId"

# text2 = "self.originBookId ?? "+'""'
# text2 = "self.originBookId"
# text2 = "SSelf.originBookId ?? "+'""'
# text2 = "SSelf.originBookId"
# text2 = "originBookId"

# text2 = "self.govId ?? "+'""'
# text2 = "self.govId"
# text2 = "SSelf.govId ?? "+'""'
# text2 = "SSelf.govId"
# text2 = "govId"
allsources = fitFiles + ["/Users/joker/Workplace/zhengwutong3/zhengwutong/appDelegate.swift"]
needEditLines = []
def aa():
    print(len([p for p in allsources if len(
            searchText(p, text1 = ".request(", text2 = text2)) > 0]))

    [[print(x,"\n",p.split("/")[-1][:-6]) for x in searchText(p, text1 = ".request(", text2 = text2)] for p in allsources if len(
            searchText(p, text1 = ".request(", text2 = text2)) > 0]

    needEditLines = [[(x[0],p) for x in searchText(p, text1 = ".request(", text2 = text2)] for p in allsources if len(
            searchText(p, text1 = ".request(", text2 = text2)) > 0]
    print(needEditLines)
text1 = "    var "+text2+":String = "+'""'
# def aa1():
#     print(len([p for p in allsources if len(
#         searchText(p, text1=".request(", text2="")) > 0 and len(searchText(p, text1=text1, text2="")) > 0]))
#     print([p for p in allsources if len(
#             searchText(p, text1=".request(", text2="")) > 0 and len(searchText(p, text1=text1, text2="")) > 0])
needEditLines = [[(x[0], p) for x in searchText(p, text1=".request(", text2="")] for p in allsources if len(
    searchText(p, text1=".request(", text2="")) > 0 and len(searchText(p, text1=text1, text2="")) > 0]
print(needEditLines)
# aa1()
def setHeader():
    if len(needEditLines) > 0:
        for items in catDirs(needEditLines):
            cs = []
            with open(items[1],"r") as f1:
                cs = f1.readlines()
                cs[items[0]] = cs[items[0]].replace(").responseZWTJSON"," ,govId:"+text2+",placeHolder11:1).responseZWTJSON")
            with open(items[1],"w") as f:
                f.writelines(cs)
setHeader()
# print([searchText2("/Users/joker/Workplace/zhengwutong3/zhengwutong/appDelegate.swift","\"A")])